package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsAttachItem;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsCarItem;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsInfoAOUForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsInfoFindForm;
import com.woniuxy.lrz.carlproject.dao.*;
import com.woniuxy.lrz.carlproject.model.*;
import com.woniuxy.lrz.carlproject.service.GoodInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.GoodPropertyService;
import com.woniuxy.lrz.carlproject.service.GoodPropertyValueService;
import com.woniuxy.lrz.carlproject.service.GoodSkuService;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarDetailsTypeBrandDto;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class GoodInfoServiceImpl extends ServiceImpl<GoodInfoMapper, GoodInfo> implements GoodInfoService {
    @Resource
    private GoodInfoMapper goodInfoMapper;
    @Resource
    private GoodAttachmentMapper goodAttachmentMapper;
    @Resource
    private GoodCarMapper goodCarMapper;
    @Resource
    private GoodSkuService goodSkuService;
    @Override
    public boolean addGoodInfo(GoodsInfoAOUForm goodsInfoAOUForm) {
        GoodInfo goodInfo = BeanUtil.copyProperties(goodsInfoAOUForm, GoodInfo.class);
        GoodInfo good_name = goodInfoMapper.selectOne(new QueryWrapper<GoodInfo>().eq("good_name", goodsInfoAOUForm.getGoodName()));
        //判断新增还是修改
        if(goodsInfoAOUForm.getGoodId()==null){
            //验证新增是否重名
            if(good_name!=null){
                throw new AppException("有重名商品，请稍后再试！");
            }
            //新增商品表(默认状态为下架，在添加SKU后，可以上架)
            goodInfo.setGoodStatus("DOWN");
            goodInfoMapper.insert(goodInfo);
            //新增商品附件表
            ArrayList<GoodsAttachItem> goodAttachItems = goodsInfoAOUForm.getGoodAttachItems();
            for (GoodsAttachItem goodAttachItem : goodAttachItems) {
                GoodAttachment goodAttachment = BeanUtil.copyProperties(goodAttachItem, GoodAttachment.class);
                goodAttachment.setGoodId(goodInfo.getGoodId());
                goodAttachmentMapper.insert(goodAttachment);
            }
            //新增商品车型中间表
            ArrayList<Integer> carDetails = goodsInfoAOUForm.getCarDetails();
            for (Integer carDetail : carDetails) {
                GoodCar goodCar=new GoodCar();
                goodCar.setGoodId(goodInfo.getGoodId());
                goodCar.setCarDetailId(carDetail);
                goodCarMapper.insert(goodCar);
            }
        }
        //验证修改后是否重名
        if(good_name!=null&&goodInfo.getGoodId()!=good_name.getGoodId()){
            throw new AppException("有重名商品，请稍后再试！");
        }
        //修改商品表
        goodInfoMapper.updateById(goodInfo);
        //修改商品附件表
        //检查是否需要修改
        ArrayList<GoodsAttachItem> goodAttachItems = goodsInfoAOUForm.getGoodAttachItems();
        if(goodAttachItems.size()>0){
            //确认修改，先删除原来的
            goodAttachmentMapper.delete(new QueryWrapper<GoodAttachment>().eq("good_id",goodInfo.getGoodId()));
            //再新增
            for (GoodsAttachItem goodAttachItem : goodAttachItems) {
                GoodAttachment goodAttachment = BeanUtil.copyProperties(goodAttachItem, GoodAttachment.class);
                goodAttachment.setGoodId(goodInfo.getGoodId());
                goodAttachmentMapper.insert(goodAttachment);
            }
        }
        //修改商品车型中间表
        //检查是否需要修改
        ArrayList<Integer> carDetails = goodsInfoAOUForm.getCarDetails();
        if(carDetails.size()>0){
            //确认修改，先删除原来的
            goodCarMapper.delete(new QueryWrapper<GoodCar>().eq("good_id",goodInfo.getGoodId()));
            //新增
            for (Integer carDetail : carDetails) {
                GoodCar goodCar=new GoodCar();
                goodCar.setGoodId(goodInfo.getGoodId());
                goodCar.setCarDetailId(carDetail);
                goodCarMapper.insert(goodCar);
            }
        }


        return true;
    }

    @Override
    public boolean deleteGoods(Integer goodId) {
        //验证是否重复删除
        GoodInfo goodInfo = goodInfoMapper.selectById(goodId);
        if(goodInfo==null){
            throw new AppException("请勿重复删除");
        }
        //删除商品
        goodInfoMapper.deleteById(goodId);
        //删除商品车型中间表
        goodCarMapper.delete(new QueryWrapper<GoodCar>().eq("good_id",goodId));
        //删除商品附件表
        goodAttachmentMapper.delete(new QueryWrapper<GoodAttachment>().eq("good_id",goodId));
        return true;
    }

    @Override
    public Page<GoodsInfoDto> findGoodsInfo(GoodsInfoFindForm goodsInfoFindForm) {
        Page<GoodInfo> page=new Page<>(goodsInfoFindForm.getPageNum(),goodsInfoFindForm.getPageSize());
        Page<GoodsInfoDto> goodsInfo = goodInfoMapper.findGoodsInfo(page, goodsInfoFindForm);
        return goodsInfo;
    }

    @Override
    public GoodsCarAndAttachDto findGoodsInfoByGoodId(Integer goodId) {
        //根据商品Id查询对应的附件信息
        ArrayList<GoodAttachment> goodAttachments = (ArrayList<GoodAttachment>)goodAttachmentMapper.selectList(new QueryWrapper<GoodAttachment>().eq("good_id", goodId));
        //根据商品ID查询对应的车型信息
        ArrayList<GoodCar> goodCars = (ArrayList<GoodCar>)goodCarMapper.selectList(new QueryWrapper<GoodCar>().eq("good_id", goodId));
        ArrayList<CarDetailsTypeBrandDto> carDetailsTypeBrandDtos=new ArrayList<>();
        for (GoodCar goodCar : goodCars) {
            CarDetailsTypeBrandDto carDetailsTypeBrandDto = goodInfoMapper.CarDetailsTypeBrandDto(goodCar.getCarDetailId());
            carDetailsTypeBrandDtos.add(carDetailsTypeBrandDto);
        }
        GoodsCarAndAttachDto goodsCarAndAttachDto=new GoodsCarAndAttachDto();
        goodsCarAndAttachDto.setGoodCars(carDetailsTypeBrandDtos);
        goodsCarAndAttachDto.setGoodAttachments(goodAttachments);
        return goodsCarAndAttachDto;
    }

    @Override
    public List<CarDetailsTypeBrandDto> loadAllCars() {
        List<CarDetailsTypeBrandDto> carDetailsTypeBrandDtos = goodInfoMapper.loadAllCars();
        return carDetailsTypeBrandDtos;
    }

    @Override
    public List<GoodsInfoByServiceAndCarDto> loadGoodsByServiceAndCar(Integer serviceId, Integer carDetailId) throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        List<GoodsInfoByServiceAndCarDto> goodsInfoByServiceAndCarDtos = goodInfoMapper.loadGoodsByServiceAndCar(serviceId, carDetailId);
        for (GoodsInfoByServiceAndCarDto goodsInfoByServiceAndCarDto : goodsInfoByServiceAndCarDtos) {
            ArrayList<GoodsSkuDto> goodsSkuDtos = goodsInfoByServiceAndCarDto.getGoodsSkuDtos();
            if(goodsSkuDtos.size()>0){
                for (GoodsSkuDto goodsSkuDto : goodsSkuDtos) {
                    GoodSku goodSku = goodSkuService.getById(goodsSkuDto.getSkuId());
                    String skuProperty = goodSku.getSkuProperty();
                    HashMap<String, String> map = (HashMap<String, String>) objectMapper.readValue(skuProperty, Map.class);
                    Set<Map.Entry<String, String>> entries = map.entrySet();
                    ArrayList<GoodsSkuPropertyItem> goodsSkuPropertyItems=new ArrayList<>();
                    for (Map.Entry<String, String> entry : entries) {
                        String key = entry.getKey();
                        String value = entry.getValue();
                        GoodsSkuPropertyItem goodsSkuPropertyItem=new GoodsSkuPropertyItem();
                        goodsSkuPropertyItem.setPropertyName(key);
                        goodsSkuPropertyItem.setPropertValue(value);
                        goodsSkuPropertyItems.add(goodsSkuPropertyItem);
                    }
                    goodsSkuDto.setGoodsSkuPropertyItems(goodsSkuPropertyItems);
                }
            }

        }
        return goodsInfoByServiceAndCarDtos;
    }


}
